package com.jackie.setting

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.IntDef
import androidx.recyclerview.widget.RecyclerView
import javax.xml.transform.Source

class SettingsAdapter(val context: Context, var list: List<SettingsItem>) :
    BaseAdapter<SettingsItem, SettingsAdapter.Companion.SettingViewHolder>(list) {
    override fun getItemViewType(position: Int): Int {
        return data[position].type
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SettingViewHolder {
        val layoutId = when (viewType) {
            NORMAL_TYPE -> R.layout.layout_settings_item
            SPECIAL_TYPE -> R.layout.layout_special
            SWITCH_TYPE -> R.layout.layout_switch_item
            else -> R.layout.layout_settings_item
        }
        val itemLayout = LayoutInflater.from(context).inflate(layoutId, parent, false)
        return SettingViewHolder(itemLayout)
    }

    override fun onBindViewHolder(holder: SettingViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        holder.setEntry(data[position])
    }

    override fun getItemCount(): Int {
        return data.size
    }

    companion object {
        const val NORMAL_TYPE = 0
        const val SWITCH_TYPE = 1
        const val SPECIAL_TYPE = 2

        @IntDef(NORMAL_TYPE, SWITCH_TYPE, SPECIAL_TYPE)
        @Retention(AnnotationRetention.SOURCE)
        annotation class SettingType

        class SettingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            private val ivIcon: ImageView by lazy {
                itemView.findViewById(R.id.icon)
            }

            private val tvName: TextView by lazy {
                itemView.findViewById(R.id.tv_item_name)
            }

            private val tvDes: TextView? by lazy {
                itemView.findViewById(R.id.tv_des)
            }

            fun setEntry(item: SettingsItem) {
                ivIcon.setImageResource(item.icon)
                tvName.setText(item.name)
                tvDes?.text = item.des
            }
        }
    }
}